/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-03-21 10:13:47
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-06-07 15:02:13
 * @FilePath: \newMould_BHZ\src\components\PCManage\detectionTask\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import messageBox from "@/components/public/messageBox"; //主页面分页
import actions from "@/actions";
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import CustomDialog from "@/components/HntManage/pouringOrder/WBS/index.vue";
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import API from '@/utils/commonRequest.js'
import {
    savePileFoundationTask, //保存
    getPileFoundationTaskByFid, //详情
    getPileFoundationTaskEquipList
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
       
        "currentRow",
        "fCompanyid",
        "showName"
    ],
    data() {
        return {
            visible: "",
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,

            loading: false,
            title: null,
            isLock: false,
            fCompanyids: null,
            fCompanynames: null,
            dialogHeight: null,
            formObj: formJs.formObj,
            WBSShow: false, // WBS
            formData: {
                userId: "", // 当前登陆用户id
                userName: "", // 当前登陆用户名称
                fId: "", // 数据fid  新增时不传或者传空 ,修改时传选中数据的fid
                fCompanyid: "", // 施工单位ID
                fCompanyname: "", // 施工单位名称
                fProjectid: "", // 项目id
                fProjectname: "", // 项目名称
                fEngineeringname: "", // 工程名称
                fEngineeringpartid: "", // 工程部位id
                fEngineeringpartname: "", // 工程部位名称
                fCheckcompany: "", // 检测公司名称
                fEquipname: "", // 检测设备
                fCheckdates: this.common.day(0), // 检测日期
                fNature:""
            },
            btnShow: true,
            currentDetail: null,
            statusNum: null,
            getList:null

        };
    },
    components: {
        publicForm,
        CustomDialog,
        FormLyout,
        messageBox
    },

    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.statusNum = this.primaryInfo.status;
        this.getList = this.primaryInfo.getList;
        this.currentDetail = this.primaryInfo.data;
        console.log(this.primaryInfo);
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
            // this.formData.fProjectid = data[0].projectId;
            // this.formData.fProjectname = data[0].projectName;


        }, true);


        this.btnShow = true;


        this.formObj[2].inputButton = true;
        if (this.statusNum == 7) { //查看
            this.getDetail({
                fId: this.currentDetail.fId
            })

            this.formObj[2].inputButton = false;
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        } else {
            API.userNature().then((res) => {
                if (res.errcode == 0) {
                    this.userNature = res.data.Nature;
                    this.formData.fNature = res.data.Nature;
                    this.formData.fCompanyid = this.primaryInfo.fCompanyid;
                    this.btnShow = true;
                    this.formObj.map(item => {
                        item.disabled = false;
                    });



                    if (this.statusNum == 1) { //新增
                        if (this.userNature == "施工单位") {
                            this.formObj[0].disabled = true;
                            this.getTree(0, this.primaryInfo.fCompanyid)
                        } else if (this.userNature == "中心试验室") {
                            this.getTender1(this.getList.fprojectid);
                            this.formObj[0].disabled = false;

                        }

                        // else if (this.userNature == "总监办") {
                        //     this.getTree(1, this.getList.fprojectid)
                        //     this.formObj[1].disabled = false;
                        // }

                        this.getTender(this.primaryInfo.fProjectid,this.primaryInfo.fCompanyid,0)



                        this.btnShow = true
                        this.formObj.map((item) => {
                            item.disabled = false;
                        });
                        // this.formObj[0].disabled = true;
                        this.formObj[4].disabled = true;

                        this.formData.fProjectid = this.userInfo.projectId; //当前登录人
                        this.formData.fProjectname = this.userInfo.projectName; //当前登录人

                    } else if (this.statusNum == 2) { //编辑


                        if (this.userNature == "施工单位") {
                            this.formObj[0].disabled = true;
                            this.formData.fCompanyname = this.currentDetail.fCompanyname; //当前登录人标段名称
                            this.formData.fCompanyid = this.currentDetail.fCompanyid; //当前登录人标段ID
                        } else if (this.userNature == "中心试验室") {
                            this.getTender1(this.getList.fprojectid);
                            this.formObj[0].disabled = false;

                        }
                        this.getTender(this.primaryInfo.fProjectid,this.primaryInfo.fCompanyid,1)

                        this.formData.fId = this.currentDetail.fId //按钮 
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                        this.formObj.map((item) => {
                            item.disabled = false;
                        });
                        this.formObj[0].disabled = true;
                        this.formObj[4].disabled = true;

                    }
                }
            }).catch((err) => {
                console.log("API", err)
            })
        }


        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
            if (b == "fCheckcompany") { //生产厂家
                this.formData.fCheckcompany = data.label; //检测单位
                this.formData.fEquipname = data.value //设备名称
            }else if (b == 'fCompanyname') {
                this.formData.fCompanyname = data.label;
                this.formData.fCompanyid = data.value;
                
            }
        },
        confirmMsg() {
            this.showMsgBox = false;
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // WBS
        clickDialog(index, prop) {
            if (prop == "fEngineeringpartname") {
                this.WBSShow = true;
            }

            return index, prop;
        },

        refreshList() {},
        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        async getTender1(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: localStorage.getItem("access_token"),
                },
            }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    console.log(res.data.data.GXCompanyList);
                    this.formObj[0].options = [];
                    data.map((item) => {
                        this.formObj[0].options.push({
                            label: item.sectionName,
                            value: item.sectionId,
                        });
                    });
                    this.formData.fCompanyname = this.formObj[0].options[0].label;
                    this.formData.fCompanyid = this.formObj[0].options[0].value;

                }
            });
        },
        async getTender(e, a, num) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                    headers: {
                        "Content-Type": "application/json",
                        Authorization: localStorage.getItem("access_token"),
                    },
                }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    console.log(data);
                    data.map(item => {

                        if (item.sectionId == a) {
                            if (num == 0) {
                                this.formData.fCompanyid = item.sectionId; //当前登录人id
                                this.formData.fCompanyname = item.sectionName; //当前登录人名称

                            }
                            this.yj_searchForm(item.sectionId)

                        }
                    })


                }
            });
        },
        yj_searchForm(data) {
            var obj = {
                fCompanyid: data, //左侧选中组织机构id

            }
            this.loading = true;
            var that = this;
            this.formObj[3].options = []
            getPileFoundationTaskEquipList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        console.log(res.data.equipList.list);


                        var info = [];
                        res.data.equipList.list.map(item => {
                            info.push({
                                label: item.fCheckcompanyname,
                                value: item.fCheckequipname
                            })
                        })
                        this.formObj[3].options = info;

                        // that.tableData = res.data.equipList.list;
                        // this.queryCriteriaData = res.data.equipList;
                    } else {
                        this.$message.error({
                            content: "列表数据获取失败" + res.msg,
                            duration: 1000
                        })
                        this.loading = false;
                    }
                })

        },
        getWBS(data) { //获取选择WBS信息

            this.formData.fEngineeringpartname = data.fLongname //工程部位
            this.formData.fEngineeringpartid = data.fNodeid //工程部位id
        },
        // WBS
        // 详情
        getDetail(obj) {
            this.loading = true;
            getPileFoundationTaskByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.pileTask
                    this.formData = data
                    if (this.statusNum == 7) {
                        this.btnShow = false
                        this.formObj.map((item) => {
                            item.disabled = true;
                        });
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm() {

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true



            this.formData.userId = this.userInfo.id; //当前登录人id
            this.formData.userName = this.userInfo.userName; //当前登录人名称




            savePileFoundationTask(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;

                        this.$router.push({
                            path: "/PCManage/detectionTask",

                        });
                        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");
                        this.$message.success({
                            content: "新增成功",
                            duration: 1000
                        })
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        console.log("接口报错");
                    }
                })
        },
    },
};